<template>
    <div class="mainDiv">
        <el-row class="rowHead">
            <el-button-group>
                <el-button type="success" icon="h-tuika2" @click="backcard" v-qx="quanxian.backcards">退卡</el-button>
                <el-button type="success" @click="backaccount" icon="h-tuikuan" v-qx="quanxian.backmoney">退款</el-button>
            </el-button-group>
            <el-button type="primary" @click="reupdate" icon="h-shuaxin1" style="float:right">刷新列表</el-button>
        </el-row>
        <el-row :gutter="20" class="rowHead">
            <el-col :span="14">
                <el-tag type="success">筛选</el-tag>
                <el-date-picker v-model="page.times" type="daterange" align="right" placeholder="请选择日期范围" :picker-options="pickerOptions2" @change="reupdate">
                </el-date-picker>
                <el-select v-model="page.zt" @change="reupdate" v-qx="quanxian.status">
                    <el-option v-for="(o, k) in zhuanttai" :label="o.txt" :value="o.val">
                    </el-option>
                </el-select>
            </el-col>
            <el-col :span="10">
                <el-input placeholder="搜索退卡人、会员卡号" v-model="page.search" @keyup.enter.native="reupdate">
                    <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="reupdate"></i>
                </el-input>
            </el-col>
        </el-row>
        <el-table :data="tableData" :stripe="true" height="100%">
            <!-- <el-table-column prop="id" label="ID"></el-table-column> -->
            <el-table-column prop="order_sn" label="退卡单号" width="180" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="ordertype" label="类型">
                <template slot-scope="scope">
                    <el-tag type="primary" size="mini" v-if="scope.row.ordertype==10">退卡</el-tag>
                    <el-tag type="success" size="mini" v-else-if="scope.row.ordertype==11">退款</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="gname" label="退卡人">
                <template slot-scope="scope">
                    <span @click="openGuestPage(scope.row.gid)" style="cursor:pointer;color:#f50">{{scope.row.gname}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="vipcard" label="会员卡号"></el-table-column>
            <el-table-column prop="money" label="退款金额"></el-table-column>
            <el-table-column prop="isstore" label="转存余额">
                <template slot-scope="scope">
                    <span class="el-icon-h-shi green" v-if="scope.row.isstore"></span>
                    <span class="el-icon-h-fou red" v-else></span>
                </template>
            </el-table-column>
            <el-table-column prop="addtime" label="退款时间" :show-overflow-tooltip="true">
                <template slot-scope="scope">{{time(scope.row.addtime)}}</template>
            </el-table-column>
            <el-table-column prop="aname" label="操作员"></el-table-column>
            <el-table-column prop="sname" label="审核员" v-if="page.zt"></el-table-column>
            <el-table-column prop="remarks" label="退卡描述" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button-group v-if="!scope.row.status">
                        <el-tooltip content="待审核" placement="left" :open-delay="800" v-qx="quanxian.check">
                            <el-button size="mini" type="primary" @click="check(scope.row)" icon="el-icon-h-shijian"></el-button>
                        </el-tooltip>
                        <el-button size="mini" type="danger" @click="delect(scope.row)" icon="el-icon-delete" v-qx="quanxian.del"></el-button>
                    </el-button-group>
                    <el-button-group v-else>
                        <el-button type="info" icon="el-icon-h-chakan" size="mini" @click="look(scope.row.id)" v-qx="quanxian.look"></el-button>
                        <el-tooltip content="反审核" placement="top" :open-delay="800" v-qx="quanxian.check">
                            <el-button type="danger" icon="el-icon-h-qiyong" size="mini" @click="fanshenhe(scope.row.id)"></el-button>
                        </el-tooltip>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" :current-page="page.pages" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
import Guest from '#/src/shop/guest.js'

export default {
    name: 'backCardList',
    data() {
        return {
            tableData: [],
            page: {
                pages: 1,
                limits: 15,
                search: '',
                zt: 0,
                times: [new Date(), new Date()],
            },
            total: 0,
            zhuanttai: [{
                txt: '未审核',
                val: 0
            }, {
                txt: '已审核',
                val: 1
            }],
            quanxian: {
                backcards: 'back_backcards',
                backmoney: 'back_backmoney',
                status: 'back_status',
                check: 'back_check',
                del: 'back_del',
                look: 'back_look',
            },
            currt: '',
            pickerOptions2: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const td = new Date()
                        td.setHours(0)
                        td.setMinutes(0)
                        td.setSeconds(0)
                        td.setMilliseconds(0)
                        const start = new Date()
                        start.setTime(td.getTime() - 3600 * 1000 * 24 * 7)
                        picker.$emit('pick', [start, td])
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const td = new Date()
                        td.setHours(0)
                        td.setMinutes(0)
                        td.setSeconds(0)
                        td.setMilliseconds(0)
                        const start = new Date()
                        start.setTime(td.getTime() - 3600 * 1000 * 24 * 30)
                        picker.$emit('pick', [start, td])
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const td = new Date()
                        td.setHours(0)
                        td.setMinutes(0)
                        td.setSeconds(0)
                        td.setMilliseconds(0)
                        const start = new Date()
                        start.setTime(td.getTime() - 3600 * 1000 * 24 * 90)
                        picker.$emit('pick', [start, td])
                    }
                }]
            }
        }
    },
    methods: {
        look: async function(id) {
            C.openBrowserWindow('/backCardInfo', { id: id }, { width: 1400, height: 880, useSpa: true, spaModel: 'shop', transparent: true, frame: false })
            this.reupdate()
        },
        backcard: async function() {
            let res = await C.openBrowserWindow('/backCard', {}, { width: 1400, height: 880, useSpa: true, spaModel: 'shop', transparent: true, frame: false })
            if (res) {
                C.success(res.data)
                this.reupdate()
            }
        },
        backaccount: async function() {
            let res = await C.openBrowserWindow('/backAccount', {}, { width: 1200, height: 800, useSpa: true, spaModel: 'shop', transparent: true, frame: false })
            if (res) {
                C.success(res.data)
                this.reupdate()
            }
        },

        time: function(unix) {
            return C.times(unix)
        },

        sizeChange: function(val) {
            this.page.limits = val
            this.reupdate()
        },

        currentChange: function(val) {
            this.page.pages = val
            this.reupdate()
        },

        // 审核退卡单
        check: async function(row) {
            let res = await C.openBrowserWindow('/checkBackCard', { id: row.id, type: row.ordertype, gid: row.gid }, { width: 1400, height: 880, useSpa: true, spaModel: 'shop', transparent: true, frame: false })
            if (res) {
                C.success(res.data)
                this.reupdate()
            }
        },

        fanshenhe: function(id) {
            C.confirm('确定反审核当前订单？', async() => {
                let res = await C.ajax('/shop/backcard/againstcheck', { id: id })
                if (res) {
                    C.success(res.data)
                    this.reupdate()
                }
            })
        },

        // 删除未结算退卡/退款单
        delect: function(row) {
            if (row.ordertype === 10) {
                C.confirm('确定删除该退卡单？', async() => {
                    let res = await C.ajax('/shop/backcard/deletebackcard', { id: row.id }, 'post')
                    if (res) {
                        C.success(res.data)
                        this.reupdate()
                    }
                })
            } else if (row.ordertype === 11) {
                C.confirm('确定删除该退款单？', async() => {
                    let res = await C.ajax('/shop/backcard/deletebackaccount', { id: row.id }, 'post')
                    if (res) {
                        C.success(res.data)
                        this.reupdate()
                    }
                })
            }
        },

        reupdate: async function() {
            let url = '/shop/backcard/backcardlist'
            let data = await C.ajax(url, { page: JSON.stringify(this.page) }, 'post')
            if (data) {
                this.total = data.count
                this.tableData = data.data
            }
        },
        openGuestPage(id) {
            Guest.openGuestPage(id)
        }
    },
    mounted: function() {
        this.reupdate()
    }
}
</script>